home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / SmallInt.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.4 KB  |  150 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import symjava.lang.Bignum;
  6. import symjava.sql.SQLException;
  7.  
  8. class SmallInt extends NumberField {
  9.    short _iVal;
  10.  
  11.    int getType() {
  12.       return 85;
  13.    }
  14.  
  15.    void readData(ServerObject data) throws SQLException, IOException, ErrorException {
  16.       this._iVal = ((NetData)data).getShort();
  17.    }
  18.  
  19.    void writeData(DataOutputStream os) throws IOException {
  20.       NetData data = new NetData(this._iVal);
  21.       data.write(os);
  22.    }
  23.  
  24.    public String getString() throws SQLException {
  25.       return ((Field)this).isNull() ? null : String.valueOf(this._iVal);
  26.    }
  27.  
  28.    public boolean getBoolean() throws SQLException {
  29.       if (((Field)this).isNull()) {
  30.          return false;
  31.       } else {
  32.          return this._iVal != 0;
  33.       }
  34.    }
  35.  
  36.    public byte getByte() throws SQLException {
  37.       return ((Field)this).isNull() ? 0 : (byte)this._iVal;
  38.    }
  39.  
  40.    public short getShort() throws SQLException {
  41.       return ((Field)this).isNull() ? 0 : this._iVal;
  42.    }
  43.  
  44.    public int getInt() throws SQLException {
  45.       return ((Field)this).isNull() ? 0 : this._iVal;
  46.    }
  47.  
  48.    public long getLong() throws SQLException {
  49.       return ((Field)this).isNull() ? 0L : (long)this._iVal;
  50.    }
  51.  
  52.    public float getFloat() throws SQLException {
  53.       return ((Field)this).isNull() ? 0.0F : (float)this._iVal;
  54.    }
  55.  
  56.    public double getDouble() throws SQLException {
  57.       return ((Field)this).isNull() ? (double)0.0F : (double)this._iVal;
  58.    }
  59.  
  60.    public Bignum getBignum(int scale) throws SQLException {
  61.       return ((Field)this).isNull() ? null : new Bignum(this._iVal, scale);
  62.    }
  63.  
  64.    public void setBoolean(boolean x) throws SQLException {
  65.       if (x) {
  66.          this._iVal = 1;
  67.       } else {
  68.          this._iVal = 0;
  69.       }
  70.  
  71.       super._null = false;
  72.    }
  73.  
  74.    public void setByte(byte x) throws SQLException {
  75.       this._iVal = (short)x;
  76.       super._null = false;
  77.    }
  78.  
  79.    public void setShort(short x) throws SQLException {
  80.       this._iVal = x;
  81.       super._null = false;
  82.    }
  83.  
  84.    public void setInt(int x) throws SQLException {
  85.       this.validateRange(x);
  86.       this._iVal = (short)x;
  87.       super._null = false;
  88.    }
  89.  
  90.    public void setLong(long x) throws SQLException {
  91.       this._iVal = (short)((int)x);
  92.       super._null = false;
  93.    }
  94.  
  95.    public void setFloat(float x) throws SQLException {
  96.       Float f = new Float(x);
  97.       int i = f.intValue();
  98.       this.validateRange(i);
  99.       this._iVal = (short)i;
  100.       super._null = false;
  101.    }
  102.  
  103.    public void setDouble(double x) throws SQLException {
  104.       Double d = new Double(x);
  105.       int i = d.intValue();
  106.       this.validateRange(i);
  107.       this._iVal = (short)i;
  108.       super._null = false;
  109.    }
  110.  
  111.    public void setBignum(Bignum x) throws SQLException {
  112.       int i = x.intValue();
  113.       this.validateRange(i);
  114.       this._iVal = (short)i;
  115.       super._null = false;
  116.    }
  117.  
  118.    public void setString(String x) throws SQLException {
  119.       Integer i = new Integer(x);
  120.       int s = i;
  121.       this.validateRange(s);
  122.       this._iVal = (short)s;
  123.       super._null = false;
  124.    }
  125.  
  126.    public int getSQLType() {
  127.       return 5;
  128.    }
  129.  
  130.    public Object getObject() throws SQLException {
  131.       return new Integer(this._iVal);
  132.    }
  133.  
  134.    public void setObject(Object obj) throws SQLException {
  135.       this.setInt((Integer)obj);
  136.    }
  137.  
  138.    void validateRange(int x) throws SQLException {
  139.       if (x < -32768 || x > 32767) {
  140.          throw new SQLException("Data out of range for SmallInt");
  141.       }
  142.    }
  143.  
  144.    void validateRange(long x) throws SQLException {
  145.       if (x < -32768L || x > 32767L) {
  146.          throw new SQLException("Data out of range for SmallInt");
  147.       }
  148.    }
  149. }
  150.